
package com.yimixian.app.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.SmsMessage;

/**
 * Created by songzhiyang on 2015/8/27.
 */
public class SmsVerifyCodeReceiver extends BroadcastReceiver {

    private SmsVerifyCodeListener mSmsVerifyCodeListener;

    @Override
    public void onReceive(Context context, Intent intent) {
        Object[] pdus = (Object[]) intent.getExtras().get("pdus");
        for (Object p : pdus) {
            byte[] pdu = (byte[]) p;
            SmsMessage message = SmsMessage.createFromPdu(pdu);
            String content = message.getMessageBody();
            String[] contents = content.split("验证码为");
            String commitNumber = contents[2].substring(0, 6);
            String senderNumber = message.getOriginatingAddress();

            if ("10690329013972".equals(senderNumber)) {
                if (mSmsVerifyCodeListener != null) {
                    mSmsVerifyCodeListener.setOnSmsVerifyCodeFillListener(commitNumber);
                }
            }
        }
    }

    public void setSmsVerifyCodeListener(SmsVerifyCodeListener listener) {
        mSmsVerifyCodeListener = listener;
    }

    public interface SmsVerifyCodeListener {
        void setOnSmsVerifyCodeFillListener(String content);
    }
}
